package com.goddess.yeecolor.activity.myFragmentItemActivityAll;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.PerfectMeansAcitivity;
import com.goddess.yeecolor.activity.ProfessionTradeActivity;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.goddess.yeecolor.tools.MyUrl.member;
import static com.goddess.yeecolor.tools.MyUrl.url;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/14
 * 作用: ******
 * 备注: 加入优雅荟的Activity
 */
public class JoinYouYaActivity extends BaseActivity {
    @InjectView(R.id.title_back)
    ImageView titleBack;
    @InjectView(R.id.et_name)
    EditText etName;
    @InjectView(R.id.et_phone)
    EditText etPhone;
    @InjectView(R.id.et_code)
    EditText etCode;
    @InjectView(R.id.btn_code)
    Button btnCode;
    @InjectView(R.id.et_wechat)
    EditText etWechat;
    @InjectView(R.id.textView3)
    TextView textView3;
    @InjectView(R.id.tv_choice_one)
    TextView tvChoiceOne;
    @InjectView(R.id.industry)
    LinearLayout industry;
    @InjectView(R.id.textView)
    TextView textView;
    @InjectView(R.id.tv_choice_to)
    TextView tvChoiceTo;
    @InjectView(R.id.position)
    LinearLayout position;
    @InjectView(R.id.cb_is)
    CheckBox cbIs;
    @InjectView(R.id.start_rule)
    TextView startRule;
    @InjectView(R.id.btn_join_submit)
    Button btnJoinSubmit;
    private Bundle bundle;

    private int Tag = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joinyouya);
        ButterKnife.inject(this);

    }

    private final int HANDLER_ONE=1;
    private final int HANDLER_TWO=2;
    private String phone;
    private Map<Object,Object>map;
    @OnClick({R.id.btn_code, R.id.industry, R.id.position, R.id.start_rule, R.id.btn_join_submit})
    public void onClick(View view) {
        Intent intent=null;
        switch (view.getId()) {
            case R.id.btn_code://获取验证码按钮
                phone=etPhone.getText().toString().trim();
                map=new HashMap<>();
                map.put("mobile",phone);
                setRequest(url+member,map,HANDLER_ONE);

                break;
            case R.id.industry://选择行业
                bundle = new Bundle();
                bundle.putString("Tag", "行业");
                JumpIntents.startIntentResultIntent(this, ProfessionTradeActivity.class, Tag, bundle);
                break;
            case R.id.position://选择职位
                bundle = new Bundle();
                bundle.putString("Tag", "职业");
                JumpIntents.startIntentResultIntent(this, ProfessionTradeActivity.class, Tag, bundle);
                break;
            case R.id.start_rule://用户协议
                break;
            case R.id.btn_join_submit://提交按钮
                intent =new Intent(JoinYouYaActivity.this,PaymentActivity.class);
                startActivity(intent);
                break;
        }

    }
    private String name;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            if (resultCode==2){//行业
                name=data.getStringExtra("name");
                textView3.setText(name);
            }
            else if (resultCode==3){
                name=data.getStringExtra("name");
                textView.setText(name);
            }
        }
    }

    private void setRequest(String url, Map<Object,Object>map,int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
    }
    private String jsonString;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 1:
                    try
                    {
                        jsonString=msg.getData().getString("result");
                        JSONObject object=new JSONObject(jsonString);
                        int code=object.getInt("code");
                        ToastUtils.toast(object.getString("msg"));
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    try
                    {
                        jsonString=msg.getData().getString("result");
                        JSONObject object=new JSONObject(jsonString);
                        int code=object.getInt("code");
                        if (code==100){
                            ToastUtils.toast(object.getString("msg"));
                            JSONObject result=object.getJSONObject("result");
                            String uniique=result.getString("unique");
                            int mobile=result.getInt("mobile");
                            /**
                             * 保存数据
                             */
                            SharePreceUtils.saveRegiter(uniique,mobile);
                            JumpIntents.startIntentCan(JoinYouYaActivity.this,PerfectMeansAcitivity.class,"完善个人资料");
                            finish();

                        }
                        else if (code==403){
                            ToastUtils.toast(object.getString("msg"));
                        }

                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }

        }
    };

    @Override
    public void initOnClck() {

    }

    @Override
    public void initFindView() {

    }

    @Override
    public void initData() {

    }
}
